package com.atguigu.gulimall.cart.controller;

import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;


@Controller
public class CartIndexController {

    @Autowired
    private CartService cartService;

    @GetMapping("getCheckItem")
    @ResponseBody
    public List<CartItem> getCheckItem(){
        List<CartItem> list = null;
        try {
            list = cartService.getCheckItem();
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return list;
    }


    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") int num){

        cartService.countItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){

        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping({"/cart.html","/"})
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);

        return "cartlist";
    }



    @GetMapping("checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("checked") boolean check){

        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }


    @RequestMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") int num,
                              RedirectAttributes redirectAttributes){

        try {
            CartItem cartItem = cartService.addCartItem(skuId,num);
            /**
             * redirectAttributes.addFlashAttribute() 这个是添加到session中，跳转后读取一次马上删除
             * redirectAttributes.addAttribute() 这个会直接添加到url后面
             */
            redirectAttributes.addAttribute("skuId",skuId);

        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //重定向而非请求转发，防止重复刷新会导致重复添加商品的问题
        return "redirect:http://cart.gulimall.com/addCartItemSuccess.html";
    }

    @RequestMapping("/addCartItemSuccess.html")
    public String addCartItemSuccess(@RequestParam("skuId") Long skuId,Model model){

        CartItem cartItem = cartService.getCartItem(skuId);

        model.addAttribute("cartItem",cartItem);
        model.addAttribute("skuNum",cartItem.getCount());

        return "success";
    }


}
